package com.huawei.boss.statemachine;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.huawei.boss.statemachine.StateMachineTest.STATE;

public class StateMachine
{
    private Map<Enum, State> states = new ConcurrentHashMap<Enum, State>();
    private Enum currState;

    public void register(Enum name, State state)
    {
        states.put(name, state);
    }

    public Collection<State> listStates()
    {
        return states.values();
    }

    public State findState(Enum stateName)
    {
        if (states.containsKey(stateName))
        {
            return states.get(stateName);
        }
        return null;
    }

    public void receive(String actionName)
    {
        if (this.currState == null)
        {
            throw new UnSupportException("当前状态为设置!");
        }
        states.get(currState).fireAction(actionName);
    }
    
    public void ready(Enum currState)
    {
        this.currState = currState;
        states.get(this.currState).enter();
    }

    public Enum getCurrentState()
    {
        return this.currState;
    }

    public void gotoState(Enum state) {
        State from = states.get(this.currState);
        State to = states.get(state);
        
        from.exit();
        this.currState = state;
        to.enter();
    }
}
